<!-- 购物卡结佣管理-购物卡库存-批量入库-->
<template>
    <div class="shoppingCardStockBulkUpload">
        <el-breadcrumb separator="/" class="bread-title">
            <el-breadcrumb-item :to="{ path: '/shoppingCardStock' }">购物卡库存</el-breadcrumb-item>
            <el-breadcrumb-item>批量导入购物卡</el-breadcrumb-item>
        </el-breadcrumb>
        <el-form class="demo-form-inline">
            <el-form-item label="下载模板:"><a :href="dowLoadPath" target="_blank" class="handleA" @click="downLoadTemplate">购物卡导入模板<span class="tips">(点击下载)</span></a>
            </el-form-item>
            <el-form-item label="选择文件:">
                <el-upload
                        class="upload-demo"
                        ref="upload"
                        :limit="1"
                        accept=".xls,.xlsx"
                        :on-change="uploadChange"
                        :on-remove="uploadRemove"
                        :action="uploadAction"
                        :file-list="fileList"
                        :auto-upload="false">
                    <el-button slot="trigger" size="small" type="primary"><i class="el-icon-upload mr10"></i>选择上传文件
                    </el-button>
                    <span slot="tip" class="tips ml10">格式支持.xls/.xlsx格式文件，文件不能超过1M</span>
                </el-upload>
            </el-form-item>
            <el-form-item label="上传文件:">
                <el-button type="primary" size="mini" @click="uploadExcel">上 传</el-button>
            </el-form-item>
            <!--:before-upload="beforeUpload"-->
            <!--:on-success="uploadSuccess"-->
            <!--:on-error="uploadError"-->
        </el-form>
    </div>
</template>

<script type="text/ecmascript-6">
    import api from '@/api/apiPath'

    export default {
        name: 'shoppingCardStockBulkUpload',
        data() {
            return {
                fileList: [],
                dowLoadPath: api.couponShoppingCardDownloadCardTemplate,
                uploadAction: api.couponShoppingCardUploadExcel
            }
        },
        created() {

        },
        mounted() {

        },
        methods: {
            downLoadTemplate() {
                this.axios.post(api.couponShoppingCardDownloadCardTemplate, {}).then((res) => {
                    console.log(res);
                })
            },
            uploadExcel() {
                if (this.fileList.length) {
                    //this.$refs.upload.submit();
                    let nameArr = this.fileList[0].name.split(".");
                    let suffix = nameArr[nameArr.length - 1]
                    if (suffix !== 'xls' && suffix !== 'xlsx') {
                        this.$message({
                            message: '请上传文件后缀为.xls/.xlsx的文件！',
                            type: 'warning'
                        });
                        return false;
                    } else {
                        let param = new FormData()
                        param.append('excelFile', this.fileList[0].raw)
                        this.axios.post(api.couponShoppingCardUploadExcel, param).then((res) => {
                            if (res.data.code === 200) {
                                this.$message({
                                    message: '导入成功',
                                    type: 'success'
                                });
                                this.$router.push({path: "./shoppingCardStock"})
                            }
                        })
                    }
                } else {
                    this.$message({
                        message: '请先选择上传文件！',
                        type: 'warning'
                    });
                }
            },
            // beforeUpload(file) {
            //     let nameArr = file.name.split(".");
            //     let suffix = nameArr[nameArr.length - 1]
            //     if (suffix !== 'xls' && suffix !== 'xlsx') {
            //         this.$message({
            //             message: '请上传文件后缀为.xls/.xlsx的文件！',
            //             type: 'warning'
            //         });
            //         return false;
            //     }
            // },
            // uploadSuccess() {
            //     this.$message({
            //         message: '上传成功！',
            //         type: 'success'
            //     });
            // },
            // uploadError() {
            //     this.$message({
            //         message: '上传失败！',
            //         type: 'warning'
            //     });
            // },
            uploadChange(file, fileList) {
                this.fileList = fileList
            },
            uploadRemove() {
                this.fileList = []
            }
        },
        components: {},
        computed: {}
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="stylus">
    .shoppingCardStockBulkUpload {
        .handleA {
            text-decoration: none;
        }

        .tips {
            color: #aaa;
        }
        .el-upload-list {
            margin-left: 66px;
            width: 200px;
        }
    }

</style>
